package net.ikenway.springboot.demo.Controller;

import com.google.gson.annotations.JsonAdapter;
import net.ikenway.springboot.demo.Model.Dto.ResponseBodyDto;
import net.ikenway.springboot.demo.Model.Dto.UserDto;
import net.ikenway.springboot.demo.Model.Entity.User;
import net.ikenway.springboot.demo.Service.Implement.UserDetailsServiceImpl;
import net.ikenway.springboot.demo.Utils.ValidateErrorMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/10/2020
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    private final UserDetailsServiceImpl userService;

    @Autowired
    UserController(UserDetailsServiceImpl userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResponseBodyDto findAll() {
        return new ResponseBodyDto(200 ,"获取用户信息成功", this.userService.findAll());
    }

    @RequestMapping(value = "/userInfo", method = RequestMethod.GET)
    public ResponseBodyDto getUserinfo() {
        return new ResponseBodyDto(200 ,"获取用户信息成功", this.userService.getUserInfo());
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBodyDto updateUserinfo(@RequestBody UserDto userDto, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) throw new Exception(ValidateErrorMsgUtil.getFirstFormatMsg(bindingResult.getAllErrors()));

            userService.update(userDto);
        } catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }
        return new ResponseBodyDto(200 ,"修改用户信息成功");
    }

    @RequestMapping(value = "/updateIdentity", method = RequestMethod.POST)
    public ResponseBodyDto updateUserIdentity(@RequestBody Map<String, String> requestData) {
        try {
            if (requestData.get("userId") == null || requestData.get("userId").isEmpty()) throw new Exception("用户ID不能为空");
            if (requestData.get("identity") == null || requestData.get("identity").isEmpty()) throw new Exception("身份不能为空");

            User.Identity identity;
            switch (requestData.get("identity")) {
                case "CUSTOMER":
                    identity = User.Identity.CUSTOMER;
                    break;
                case "MANAGER":
                    identity = User.Identity.MANAGER;
                    break;
                default:
                    throw new Exception("身份不合法");
            }

            userService.updateIdentity(identity, Long.valueOf(requestData.get("userId")));
        } catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }
        return new ResponseBodyDto(200 ,"修改用户身份成功");

    }

    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ResponseBodyDto enable(@RequestBody Map<String, List<Long>> requestData) {
        try {
            userService.enable(requestData.get("userIds"));
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "不存在的用户，已回滚操作");
        }
        return new ResponseBodyDto(200, "已启用用户");
    }

    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public ResponseBodyDto disable(@RequestBody Map<String, List<Long>> requestData) {
        try {
            userService.disable(requestData.get("userIds"));
        } catch (EntityNotFoundException entityNotFoundException) {
            return new ResponseBodyDto(500, "不存在的用户，已回滚操作");
        }
        return new ResponseBodyDto(200, "已停用用户");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseBodyDto delete() {
        try {
            userService.delete();
        }catch (Exception exception) {
            return new ResponseBodyDto(500, exception.getMessage());
        }
        return new ResponseBodyDto(200, "注销用户成功");
    }
}
